package flc.ast.fragment;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.screenshot.ResolutionMode;
import com.stark.screenshot.ScreenRecCmd;
import com.stark.screenshot.ScreenRecordService;
import com.stark.screenshot.ScreenRecordState;
import com.stark.screenshot.ScreenRecorder;
import com.stark.screenshot.ScreenShotEntry;
import com.stark.screenshot.SettingManager;

import flc.ast.R;
import flc.ast.databinding.FragmentScreenRecordBinding;
import flc.ast.dialog.ScreenRecordDialog;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.TimeUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ScreenRecordFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScreenRecordFragment extends BaseNoModelFragment<FragmentScreenRecordBinding> {

    private ScreenRecorder mScreenRecorder = ScreenRecorder.getInstance();
    private int resolutionPos, voicePos, orientationPos, floatPos = 1;
    private Handler mHander = new Handler();
    private Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            long duration = mScreenRecorder.getRecDuration();
            String strTime = TimeUtil.getHHmmss(duration);
            mDataBinding.tvDuration.setText(strTime);
            mHander.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.fragment_screen_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.llResolution.setOnClickListener(this);
        mDataBinding.llVoice.setOnClickListener(this);
        mDataBinding.llOrientation.setOnClickListener(this);
        mDataBinding.llFloat.setOnClickListener(this);
        mDataBinding.ivStartRecord.setOnClickListener(this);

        SettingManager.setResolutionMode(ResolutionMode.RES_1080P);
        SettingManager.setRecMute(false);
        SettingManager.setIsLandscape(false);

        ScreenShotEntry.sNotifyEnterActivityClass = getActivity().getClass();
        mScreenRecorder.addStateListener(mStateListener);
        mStateListener.onStateChanged(mScreenRecorder.getRecordState());
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llResolution) {//清晰度
            showResolutionDialog();
        } else if (id == R.id.llVoice) {//录制声音设置
            showVoiceDialog();
        } else if (id == R.id.llOrientation) {//录屏方向
            showOrientationDialog();
        } else if (id == R.id.llFloat) {//录屏悬浮窗
            showFloatDialog();
        } else if (id == R.id.ivStartRecord) {//开始/停止录制
            clickRecording();
        }
    }

    @Override
    protected void initData() {

    }

    private ScreenRecorder.IStateListener mStateListener = new ScreenRecorder.IStateListener() {
        @Override
        public void onStateChanged(ScreenRecordState state) {
            if (state == ScreenRecordState.RECORDING) {
                //录制中
                mHander.post(mTaskUpdateTime);
                mDataBinding.ivStartRecord.setImageResource(R.drawable.ajslp);
                mDataBinding.tvDuration.setVisibility(VISIBLE);
            } else if (state == ScreenRecordState.PAUSE) {
                //录制暂停
                mHander.removeCallbacks(mTaskUpdateTime);
            } else {
                //录制结束
                mDataBinding.ivStartRecord.setImageResource(R.drawable.akslp);
                mDataBinding.tvDuration.setVisibility(GONE);
                mHander.removeCallbacks(mTaskUpdateTime);
                String filePath = mScreenRecorder.getRecFilePath();
                if (filePath != null) {
                    FileP2pUtil.copyPrivateVideoToPublic(mContext, filePath);
                    ToastUtils.showShort(R.string.save_album_shot_tips);
                }
            }
        }
    };

    private void clickRecording() {
        if (mScreenRecorder.getRecordState() == ScreenRecordState.IDLE) {
            ScreenRecordState state = mScreenRecorder.getRecordState();
            if (state == ScreenRecordState.IDLE) {
                ScreenShotEntry.screenRecStartRecord(ScreenRecordService.class);
            }
        } else {
            if (mScreenRecorder.getRecDuration() < 5000) {
                ToastUtils.showShort(R.string.record_limit);
            } else {
                ScreenShotEntry.screenRecExcCmd(ScreenRecordService.class, ScreenRecCmd.STOP_REC);
            }
        }
    }

    private void showResolutionDialog() {
        ScreenRecordDialog dialog = new ScreenRecordDialog(mContext);
        dialog.setType(0);
        dialog.setmCurrentPos(resolutionPos);
        dialog.setListener(new ScreenRecordDialog.IListener() {
            @Override
            public void clickItem(int position, String name) {
                resolutionPos = position;
                mDataBinding.tvResolution.setText(name);
                switch (position) {
                    case 0:
                        SettingManager.setResolutionMode(ResolutionMode.RES_1080P);
                        break;
                    case 1:
                        SettingManager.setResolutionMode(ResolutionMode.RES_720P);
                        break;
                    case 2:
                        SettingManager.setResolutionMode(ResolutionMode.RES_480P);
                        break;
                    case 3:
                        SettingManager.setResolutionMode(ResolutionMode.RES_2K);
                        break;
                    default:
                        break;
                }
            }
        });
        dialog.show();
    }

    private void showVoiceDialog() {
        ScreenRecordDialog dialog = new ScreenRecordDialog(mContext);
        dialog.setType(1);
        dialog.setmCurrentPos(voicePos);
        dialog.setListener(new ScreenRecordDialog.IListener() {
            @Override
            public void clickItem(int position, String name) {
                voicePos = position;
                mDataBinding.tvVoice.setText(name);
                SettingManager.setRecMute(position == 1);
            }
        });
        dialog.show();
    }

    private void showOrientationDialog() {
        ScreenRecordDialog dialog = new ScreenRecordDialog(mContext);
        dialog.setType(2);
        dialog.setmCurrentPos(orientationPos);
        dialog.setListener(new ScreenRecordDialog.IListener() {
            @Override
            public void clickItem(int position, String name) {
                orientationPos = position;
                mDataBinding.tvOrientation.setText(name);
                SettingManager.setIsLandscape(position == 0);
            }
        });
        dialog.show();
    }

    private void showFloatDialog() {
        ScreenRecordDialog dialog = new ScreenRecordDialog(mContext);
        dialog.setType(3);
        dialog.setmCurrentPos(floatPos);
        dialog.setListener(new ScreenRecordDialog.IListener() {
            @Override
            public void clickItem(int position, String name) {
                floatPos = position;
                mDataBinding.tvFloat.setText(name);
                ScreenShotEntry.screenRecShowFloatOrNot(ScreenRecordService.class, position == 0);
            }
        });
        dialog.show();
    }

}